﻿using DevExpress.Xpf.Core;
using DevExpress.Xpf.Core.Native;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Configuration;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Input;

namespace WpfApp1.Themes
{
    public class PaletteHelper
    { 
        private const string DefaultTheme = nameof(DefaultTheme);
        private const string DevExpressTheme = nameof(DevExpressTheme);

        /// <summary>
        /// 当前主题
        /// </summary>
        public static BaseThemeType ThemeType
        {
            get
            {
                var themeType = ConfigurationManager.AppSettings[DefaultTheme];
                if (string.IsNullOrEmpty(themeType))
                {
                    return BaseThemeType.Blue;
                }
                return (BaseThemeType)Enum.Parse(typeof(BaseThemeType), themeType);
            }
            set
            { 
                Configuration config =  ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
                config.AppSettings.Settings.Remove(DefaultTheme);
                config.AppSettings.Settings.Add(DefaultTheme, value.ToString()); 
                config.Save(ConfigurationSaveMode.Modified);
                ConfigurationManager.RefreshSection("appSettings");
            }
        }

        /// <summary>
        /// 当前Dev主题
        /// </summary>
        public static string DevThemeType
        {
            get
            {
                var themeType = ConfigurationManager.AppSettings[DevExpressTheme];
                if (string.IsNullOrEmpty(themeType))
                {
                    return themeType;
                }
                return Theme.Office2019Colorful.Name;
            }
            set
            {
                Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
                config.AppSettings.Settings.Remove(DevExpressTheme);
                config.AppSettings.Settings.Add(DevExpressTheme, value.ToString());
                config.Save(ConfigurationSaveMode.Modified);
                ConfigurationManager.RefreshSection("appSettings");
            }
        }

        /// <summary>
        /// 主题样式切换
        /// </summary>
        /// <param name="theme"></param>
        public static void ChangeTheme(string theme)
        {
            var themeType = (BaseThemeType)Enum.Parse(typeof(BaseThemeType), theme);
            ChangeTheme(themeType);
        }

        /// <summary>
        /// 主题样式切换
        /// </summary>
        /// <param name="themeType"></param>
        public static void ChangeTheme(BaseThemeType themeType)
        {
            var resourceDictionarys = GetResourceDictionaries("ThemeDesign.xaml");

            BaseTheme baseTheme = new BaseTheme();
            foreach (var resourceDictionary in resourceDictionarys)
            {
                ResourceDictionary dict = new ResourceDictionary()
                {
                    Source = new Uri($"pack://application:,,,/Themes/Theme{themeType.ToString()}.xaml")
                };
                resourceDictionary.MergedDictionaries.Clear();
                resourceDictionary.MergedDictionaries.Add(dict);

                baseTheme = resourceDictionary.SetTheme(themeType); 
            }
            ThemeType = themeType;
            SetDevTheme(baseTheme);  
        }

        /// <summary>
        /// Dev样式
        /// </summary>
        private static void SetDevTheme(BaseTheme theme)
        {
            try
            { 
                var customtheme = ThemeType == BaseThemeType.Dark || ThemeType == BaseThemeType.DarkGreen ? Theme.Office2019DarkGray : Theme.Office2019Colorful; 
                ApplicationThemeHelper.ApplicationThemeName = customtheme.Name;
                DevThemeType = customtheme.Name;
            }
            catch (Exception ex)
            {
                var t = ex;
            }
        }

        /// <summary>
        /// 遍历资源
        /// </summary>
        /// <param name="key"></param>
        /// <returns></returns>
        private static List<ResourceDictionary> GetResourceDictionaries(string key)
        {
            var mds = Application.Current.Resources.MergedDictionaries;
            var md = GetResourceDictionaries(mds, key);
            return md;
        }

        /// <summary>
        /// 遍历资源
        /// </summary>
        /// <param name="resources"></param>
        /// <param name="rdName"></param>
        /// <returns></returns>
        private static List<ResourceDictionary> GetResourceDictionaries(Collection<ResourceDictionary> resources, string rdName)
        {
            List<ResourceDictionary> rd = new List<ResourceDictionary>();
            foreach (var d in resources)
            {
                if (d.Source == null) { continue; }
                if (d.Source.AbsolutePath.Contains(rdName))
                {
                    rd.Add(d);
                }
                if (d.MergedDictionaries.Any())
                {
                    var dd = GetResourceDictionaries(d.MergedDictionaries, rdName);
                    rd.AddRange(dd);
                }
            }
            return rd;
        }
    }
}
